package cn.edu.jxau.core.aop;

import net.sf.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

/**
 * Desc:
 * ------------------------------------
 * Author:fulei04@meituan.com
 * Date:2017/12/20
 * Time:下午2:38
 */
public class ProxyChain<T> {

    private Class<T> targetClass;
    private T target;
    private Method targetMethod;
    private MethodProxy methodProxy;
    private Object[] methodParams;

    private int proxyIndex;
    private List<Proxy> proxyList = new ArrayList<>();

    public ProxyChain(Class<T> targetClass, T target, Method targetMethod,
                      MethodProxy methodProxy, Object[] methodParams, List<Proxy> proxyList) {

        this.targetClass = targetClass;
        this.target = target;
        this.targetMethod = targetMethod;
        this.methodProxy = methodProxy;
        this.methodParams = methodParams;
        this.proxyList = proxyList;
    }

    public Class<T> getTargetClass() {
        return targetClass;
    }

    public Method getTargetMethod() {
        return targetMethod;
    }

    public Object[] getMethodParams() {
        return methodParams;
    }

    public Object doProxyChain() throws Throwable {

        if(proxyIndex<proxyList.size()) {
            return proxyList.get(proxyIndex++).doPorxy(this);
        } else {
            return methodProxy.invokeSuper(target,methodParams);
        }
    }
}
